package mysql

import (
	"bluebell/models"
	"go.uber.org/zap"
	"gorm.io/gorm"
)

// GetCommunityList 查询分类社区列表
func GetCommunityList() (communityList []*models.CommunityList, err error){
	communityList = []*models.CommunityList{}
	tx := db.Model(&models.Community{}).Select("community_id","community_name").Find(&communityList)
	if tx.Error != nil{
		if tx.Error == gorm.ErrRecordNotFound{
			zap.L().Warn("there is no community in db")
			err = nil
		}else{
			zap.L().Error("logic mysql failed",zap.Error(tx.Error))
			return nil,tx.Error
		}
	}
	return communityList, nil
}

// GetCommunityByID 根据ID查询分类社区详情
func GetCommunityByID(communityId uint64) (*models.CommunityDetailRes, error){
	community := new(models.CommunityDetailRes)
	tx := db.Model(&models.Community{}).Where("community_id = ?",communityId).Take(&community)
	if tx.Error != nil && tx.Error != gorm.ErrRecordNotFound{
		zap.L().Error("query community failed",zap.Error(tx.Error))
		return nil,tx.Error
	}
	return community,nil
}